<?php

class Core_Agent_Component_Customer_Controller_Shop extends Dcmp_Component_Controller {
	
	private $_poi = NULL;
	
	protected function initialize(){
		$poiid = $this->getRequest()->getParameter('poiid', NULL);
		if(!is_null($poiid)){
			$this->_poi = App::getModel('foundation/poi')->load($poiid);
			$this->getPage()->getNavigation()->addLabel($this->_poi->getShopName());
		}
	}	

    public function createAction($poiid) {
        $poi = App::getModel('foundation/poi')->load($poiid);
        return $this->viewResult(array('poi' => $poi));
    }

    public function editAction($poiid) {
        $model = App::getModel('customer/shop')->loadByPoi($poiid);
        if($model->getId()){
            return $this->viewResult(array('poi' => $model->getPoiInfo(), 'model' => $model));
        }
        return $this->redirectResult($this->getUrl(array('poiid'=>$poiid), 'create'));
    }

    public function savePostAction($status) {
    	if(!in_array($status, array('draft', 'pending'))){
    		$status = 'draft';
    	}
        return $this->saveModel($status);
    }

    public function publishPostAction($ids) {
        App::getModel('customer/shop')->publish($ids);
        return $this->redirectResult($this->getHistoryUrl('customer/index/index'));
    }

    public function forbidPostAction($ids, $statusmessage) {
        App::getModel('customer/shop')->forbid($ids, $statusmessage);
        return $this->redirectResult($this->getHistoryUrl('customer/index/index'));
    }

    private function saveModel($status) {
        $model = App::getModel('customer/shop');
        $data = $this->getRequest()->getForm('model');
         if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)
        	  ->setStatus($status);
      	if(isset($data['signmap_picture'])){
      		$model->setSignmapPicture(basename($data['signmap_picture']));
      	}
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            Kd_Util_IO::moveFiles(Kd_Web_Util::urlToPath($data['signmap_picture']),
            					  DP_PUB.'customer'.DS.'shop'.DS.$model->getId());
            return $this->redirectResult($this->getHistoryUrl('customer/index/index'), $this->__('share.message.success'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction($model->getPoiId())->setModel($model);
        }
    }

}